******************************************************************************************************
********** THIS .DO FILE PRODUCES TABLE 3 (RD Estimates of the effect of a left-wing mayor) **********
******************************************************************************************************

clear *

use "${root}/data/processed/final_sample.dta", clear

keep if baseline_sample == 1

********* Perform (robust bias-corrected) RD estimates and produce results table **********

foreach outcome in tax_rev_avg_share ///
			state_transf_avg_share ///
			fed_transf_avg_share ///
			tax_rev_avg_sgdp ///
			state_transf_avg_sgdp ///
			fed_transf_avg_sgdp ///
			tot_rev_avg_sgdp{

	*** (1) baseline (whole sample, average over 4-years, CCT 2014 MSE-optimal bandwidth)
	qui rdrobust res_`outcome' margin_mayor_left, vce(cluster mun_code) all

	local coeff1_`outcome' : di %9.2f (e(tau_bc))
	local se1_`outcome'    : di %9.2f (e(se_tau_rb))
	local pv1_`outcome'    : di %9.2f (e(pv_rb))
	local n1_`outcome' 	   = e(N)
	local effn1_`outcome'  = e(N_h_l) + e(N_h_r)

	*** (2) lame-ducks subsample (4-years average)
	qui rdrobust res_`outcome' margin_mayor_left if lame_duck == 1, vce(cluster mun_code) all

	local coeff2_`outcome'  : di %9.2f (e(tau_bc))
	local se2_`outcome' 	: di %9.2f (e(se_tau_rb))
	local pv2_`outcome' 	: di %9.2f (e(pv_rb))
	local n2_`outcome' 		= e(N)
	local effn2_`outcome' 	= e(N_h_l) + e(N_h_r)


	*** (3) Tiebout-below-median subsample (4-years average)
	qui rdrobust res_`outcome' margin_mayor_left if tiebout_median_sample == 1, vce(cluster mun_code) all

	local coeff3_`outcome'  : di %9.2f (e(tau_bc))
	local se3_`outcome' 	: di %9.2f (e(se_tau_rb))
	local pv3_`outcome' 	: di %9.2f (e(pv_rb))
	local n3_`outcome' 		= e(N)
	local effn3_`outcome' 	= e(N_h_l) + e(N_h_r)


	*** (4) Tiebout-below-75th percentile subsample (4-years average)
	qui rdrobust res_`outcome' margin_mayor_left if coal_dist_median_sample == 1, vce(cluster mun_code) all

	local coeff4_`outcome'  : di %9.2f (e(tau_bc))
	local se4_`outcome' 	: di %9.2f (e(se_tau_rb))
	local pv4_`outcome'     : di %9.2f (e(pv_rb))
	local n4_`outcome' 		= e(N)
	local effn4_`outcome' 	= e(N_h_l) + e(N_h_r)


*** (5) Positive Oil-windfalls subsample
	qui rdrobust res_`outcome' margin_mayor_left if oil_sample == 1, vce(cluster mun_code) all

	local coeff5_`outcome' : di %9.2f (e(tau_bc))
	local se5_`outcome'    : di %9.2f (e(se_tau_rb))
	local pv5_`outcome'    : di %9.2f (e(pv_rb))
	local n5_`outcome'	   = e(N)
	local effn5_`outcome'  = e(N_h_l) + e(N_h_r)
	
}


* write table
texdoc init "${root}/results/tables/table_revenue_detailed.tex", replace force

tex \caption{RD estimates of the effect of a left-wing mayor on the composition of revenues}
tex \resizebox{\linewidth}{!}{
tex \begin{tabularx}{\linewidth}{l *5{>{\Centering}X}}
tex \toprule

tex 													&        Baseline 				& \multicolumn{4}{c}{Subsamples} 								\\
tex \cmidrule(lr){2-2} \cmidrule(lr){3-6}

tex 													& 								& 	Lame Duck 			& Tiebout $<$ median	 								& Ideology distance $>$ median  						& Oil windfall					\\
tex \midrule


tex \multicolumn{6}{c}{Revenues, \% GDP} \\
tex \midrule

tex Total revenue 					& `coeff1_tot_rev_avg_sgdp'`stars1_tot_rev_avg_sgdp' 	& `coeff2_tot_rev_avg_sgdp'`stars4_tot_rev_avg_sgdp' 	& `coeff3_tot_rev_avg_sgdp'`stars5_tot_rev_avg_sgdp' 	& `coeff4_tot_rev_avg_sgdp'`stars6_tot_rev_avg_sgdp' 	& `coeff5_tot_rev_avg_sgdp'`stars7_tot_rev_avg_sgdp' 	\\
tex  										& (`se1_tot_rev_avg_sgdp') 									& (`se2_tot_rev_avg_sgdp') 						 			& (`se3_tot_rev_avg_sgdp') 						 			& (`se4_tot_rev_avg_sgdp') 						 			& (`se5_tot_rev_avg_sgdp') 									\\
tex \multicolumn{6}{l}{of which:} \\
tex \hspace{0.20cm} Municipal taxes 	& `coeff1_tax_rev_avg_sgdp'`stars1_tax_rev_avg_sgdp' 			& `coeff2_tax_rev_avg_sgdp'`stars4_tax_rev_avg_sgdp' 			& `coeff3_tax_rev_avg_sgdp'`stars5_tax_rev_avg_sgdp' 			& `coeff4_tax_rev_avg_sgdp'`stars6_tax_rev_avg_sgdp' 			& `coeff5_tax_rev_avg_sgdp'`stars7_tax_rev_avg_sgdp' 			\\
tex  										& (`se1_tax_rev_avg_sgdp') 										& (`se2_tax_rev_avg_sgdp') 										& (`se3_tax_rev_avg_sgdp') 										& (`se4_tax_rev_avg_sgdp') 										& (`se5_tax_rev_avg_sgdp') 										\\
tex \hspace{0.20cm} Federal transfers 	& `coeff1_fed_transf_avg_sgdp'`stars1_fed_transf_avg_sgdp' 		& `coeff2_fed_transf_avg_sgdp'`stars4_fed_transf_avg_sgdp' 		& `coeff3_fed_transf_avg_sgdp'`stars5_fed_transf_avg_sgdp' 		& `coeff4_fed_transf_avg_sgdp'`stars6_fed_transf_avg_sgdp' 		& `coeff5_fed_transf_avg_sgdp'`stars7_fed_transf_avg_sgdp' 		\\
tex  										& (`se1_fed_transf_avg_sgdp') 									& (`se2_fed_transf_avg_sgdp') 									& (`se3_fed_transf_avg_sgdp') 									& (`se4_fed_transf_avg_sgdp') 									& (`se5_fed_transf_avg_sgdp') 									\\
tex \hspace{0.20cm} State transfers 			& `coeff1_state_transf_avg_sgdp'`stars1_state_transf_avg_sgdp' 			& `coeff2_state_transf_avg_sgdp'`stars4_state_transf_avg_sgdp' 			& `coeff3_state_transf_avg_sgdp'`stars5_state_transf_avg_sgdp' 			& `coeff4_state_transf_avg_sgdp'`stars6_state_transf_avg_sgdp' 			& `coeff5_state_transf_avg_sgdp'`stars7_state_transf_avg_sgdp' 			\\
tex  										& (`se1_state_transf_avg_sgdp') 									& (`se2_state_transf_avg_sgdp') 									& (`se3_state_transf_avg_sgdp') 									& (`se4_state_transf_avg_sgdp') 									& (`se5_state_transf_avg_sgdp') 									\\

tex \midrule
tex \multicolumn{6}{c}{Composition of revenues: revenue categories (\% of total revenues)} \\
tex \midrule

tex Municipal taxes 	& `coeff1_tax_rev_avg_share'`stars1_tax_rev_avg_share' 			& `coeff2_tax_rev_avg_share'`stars4_tax_rev_avg_share' 			& `coeff3_tax_rev_avg_share'`stars5_tax_rev_avg_share' 			& `coeff4_tax_rev_avg_share'`stars6_tax_rev_avg_share' 			& `coeff5_tax_rev_avg_share'`stars7_tax_rev_avg_share' 			\\
tex  										& (`se1_tax_rev_avg_share') 										& (`se2_tax_rev_avg_share') 										& (`se3_tax_rev_avg_share') 										& (`se4_tax_rev_avg_share') 										& (`se5_tax_rev_avg_share') 										\\
tex Federal transfers 	& `coeff1_fed_transf_avg_share'`stars1_fed_transf_avg_share' 		& `coeff2_fed_transf_avg_share'`stars4_fed_transf_avg_share' 		& `coeff3_fed_transf_avg_share'`stars5_fed_transf_avg_share' 		& `coeff4_fed_transf_avg_share'`stars6_fed_transf_avg_share' 		& `coeff5_fed_transf_avg_share'`stars7_fed_transf_avg_share' 		\\
tex  										& (`se1_fed_transf_avg_share') 									& (`se2_fed_transf_avg_share') 									& (`se3_fed_transf_avg_share') 									& (`se4_fed_transf_avg_share') 									& (`se5_fed_transf_avg_share') 									\\
tex State transfers 			& `coeff1_state_transf_avg_share'`stars1_state_transf_avg_share' 			& `coeff2_state_transf_avg_share'`stars4_state_transf_avg_share' 			& `coeff3_state_transf_avg_share'`stars5_state_transf_avg_share' 			& `coeff4_state_transf_avg_share'`stars6_state_transf_avg_share' 			& `coeff5_state_transf_avg_share'`stars7_state_transf_avg_share' 			\\
tex  										& (`se1_state_transf_avg_share') 									& (`se2_state_transf_avg_share') 									& (`se3_state_transf_avg_share') 									& (`se4_state_transf_avg_share') 									& (`se5_state_transf_avg_share') 									\\

tex \midrule
tex Observations (all) 						&  `n1_tot_rev_avg_sgdp'    										& `n2_tot_rev_avg_sgdp'  											& `n3_tot_rev_avg_sgdp'  											& `n4_tot_rev_avg_sgdp'  											& `n5_tot_rev_avg_sgdp'  											\\
tex Observations (effective)				&  `effn1_tot_rev_avg_sgdp'										& `effn2_tot_rev_avg_sgdp'										& `effn3_tot_rev_avg_sgdp'   										& `effn4_tot_rev_avg_sgdp'										& `effn5_tot_rev_avg_sgdp'   										\\

tex \bottomrule
tex \end{tabularx}}

texdoc close
